package com.ohc.leetcode.二叉树;

import com.ohc.leetcode.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 2022年4月20日19:30:17
 */
public class 二叉树的前序遍历144 {
    private static List<Integer> list = new ArrayList();

    public static List<Integer> preorderTraversal(TreeNode root) {
        if (root == null) {
            return null;
        }
        list.add(root.val);
        preorderTraversal(root.left);
        preorderTraversal(root.right);

        return list;
    }

    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode();
        TreeNode treeNode2 = new TreeNode();
        TreeNode treeNode3 = new TreeNode();
        treeNode1.left = null;
        treeNode1.right = treeNode2;
        treeNode2.left = treeNode3;
        treeNode2.right = null;
        treeNode3.right = null;
        treeNode3.left = null;
        treeNode1.val = 1;
        treeNode2.val = 2;
        treeNode3.val = 3;
        preorderTraversal(treeNode1);
        list.forEach(e -> {
            System.out.println("e = " + e);
        });
    }
}
